package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.bean.Account;
import com.lanou.bean.Cost;
import com.lanou.bean.Service;
import com.lanou.mapper.ServiceMapper;
import com.lanou.service.AccountService;
import com.lanou.service.CostService;
import com.lanou.service.ServiceService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 18/3/13.
 */
@Controller
public class ServiceController {
    @Resource
    private ServiceService serviceService;
    @Resource
    private AccountService accountService;
    @Resource
    private CostService costService;

    @RequestMapping(value = "/servicelist")
    public String service() {
        return "service/service_list";
    }

    @ResponseBody
    @RequestMapping(value = "/listservice")
    public List<Service> listservice() {
//        System.out.println(serviceService.allservice());
        return serviceService.allservice();
    }

    @ResponseBody
    @RequestMapping(value = "/servicePage")
    public PageInfo<Service> queryAccountByPage(@RequestParam("pagenum") Integer pageNo) {
        return serviceService.queryServiceByPage(pageNo, 5);
    }

    @RequestMapping(value = "/serviceAdd")
    public String serviceAdd() {
        return "service/service_add";
    }

    @ResponseBody
    @RequestMapping(value = "/idcardNologinNameisexist")
    public boolean idcardNologinNameisexist(@RequestParam("idcardNo") String idcardNo, @RequestParam("loginName") String loginName) {
        Account account = accountService.idcardNologinNameisexist(idcardNo, loginName);
        Integer accountId = account.getAccountId();
        if (null != accountId) {
            return true;
        } else {
            return false;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/osUsernameisexist")
    public boolean osUsernameisexist(@RequestParam("osUsername") String osUsername) {
        String osUserName = serviceService.osUsernameisexist(osUsername);
        if (osUserName == null || osUserName.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/accountIdByidcardNologinName")
    public Integer accountIdByidcardNologinName(@RequestParam("idcardNo") String idcardNo, @RequestParam("loginName") String loginName) {
        Account account = accountService.idcardNologinNameisexist(idcardNo, loginName);
        Integer accountId = account.getAccountId();
        return accountId;
    }

    @ResponseBody
    @RequestMapping(value = "/selectCostIdByName")
    public List<Cost> selectCostIdByName(@RequestParam("name") String name) {
        return costService.selectCostIdByName(name);
    }

    @ResponseBody
    @RequestMapping(value = "/addservice")
    public void addservice(@RequestParam("costId") Integer costId, @RequestParam("accountId") Integer accountId, @RequestParam("unixHost") String unixHost, @RequestParam("osUsername") String osUsername, @RequestParam("loginPasswd") String loginPasswd, String status, Date createDate) {
        createDate = new Date(System.currentTimeMillis());
        status = "0";
        Service service = new Service(accountId, unixHost, osUsername, loginPasswd, costId, status, createDate);
        serviceService.addservice(service);
    }

    @ResponseBody
    @RequestMapping(value = "/delservice")
    public void delservice(@RequestParam("serviceId") Integer serviceId) {
        serviceService.deleteservice(serviceId);
    }

    @RequestMapping(value = "/serviceupdate")
    public String serviceupdate() {
        return "service/service_list";
    }


    @ResponseBody
    @RequestMapping(value = "/updateStatusByServiceId")
    public void updateStatusByServiceId(@RequestParam("status") String status, @RequestParam("serviceId") Integer serviceId) {
        serviceService.updateStatusByServiceId(status, serviceId);
    }

    @ResponseBody
    @RequestMapping(value = "/delservice")
    public void delservice(@RequestParam("serviceId") Integer serviceId) {
        serviceService.deleteservice(serviceId);
    }

    @RequestMapping(value = "/serviceupdate")
    public String serviceupdate() {
        return "service/service_modi";
    }

    @ResponseBody
    @RequestMapping(value = "/findServiceById")
    public List<Service> findServiceById(@RequestParam("serviceId") Integer serviceId) {
        return serviceService.findServiceById(serviceId);
    }

    @ResponseBody
    @RequestMapping(value = "/updateCostIdByServiceId")
    public void updateCostIdByServiceId(@RequestParam("costId") Integer costId, @RequestParam("serviceId") Integer serviceId) {
        serviceService.updateCostIdByServiceId(costId, serviceId);
    }

    @RequestMapping(value = "/servicedetail")
    public String servicedetail() {
        return "service/service_detail";
    }

    @ResponseBody
    @RequestMapping(value = "/selectByServiceId")
    public List<Service> selectByServiceId(@RequestParam("serviceId") Integer serviceId) {
        return serviceService.selectByServiceId(serviceId);
    }
}
